﻿namespace _5._9._02.PerformanceComparison
{
    using System;
    using System.Diagnostics;

    class PerformanceComparisonMain
    {
        public const int ONE_MILLION = 1000000;

        static void Int()
        {
            Stopwatch sw = new Stopwatch();

            Console.WriteLine("--- int ---");
            int add = 1;
            int subtract = int.MaxValue;
            int increment = 3;
            int multiply = 5;
            int divide = int.MaxValue;

            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                add += i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - add");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                subtract -= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - subtract");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                increment++;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - increment");

            sw.Reset();
            sw.Start();
            for (int i = 1; i <= ONE_MILLION; i++)
            {
                multiply *= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - multiply");

            sw.Reset();
            sw.Start();
            for (int i = 1; i <= ONE_MILLION; i++)
            {
                divide /= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - divide");
        }

        static void Long()
        {
            Stopwatch sw = new Stopwatch();

            Console.WriteLine("--- long ---");
            long add = 1;
            long subtract = long.MaxValue;
            long increment = 3;
            long multiply = 5;
            long divide = long.MaxValue;

            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                add += i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - add");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                subtract -= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - subtract");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                increment++;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - increment");

            sw.Reset();
            sw.Start();
            for (int i = 1; i <= ONE_MILLION; i++)
            {
                multiply *= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - multiply");

            sw.Reset();
            sw.Start();
            for (int i = 1; i <= ONE_MILLION; i++)
            {
                divide /= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - divide");
        }

        static void Float()
        {
            Stopwatch sw = new Stopwatch();

            Console.WriteLine("--- float ---");
            float add = 1;
            float subtract = long.MaxValue;
            float increment = 3;
            float multiply = 5;
            float divide = long.MaxValue;

            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                add += i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - add");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                subtract -= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - subtract");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                increment++;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - increment");

            sw.Reset();
            sw.Start();
            for (int i = 1; i <= ONE_MILLION; i++)
            {
                multiply *= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - multiply");

            sw.Reset();
            sw.Start();
            for (int i = 1; i <= ONE_MILLION; i++)
            {
                divide /= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - divide");
        }

        static void Double()
        {
            Stopwatch sw = new Stopwatch();

            Console.WriteLine("--- double ---");
            double add = 1;
            double subtract = long.MaxValue;
            double increment = 3;
            double multiply = 5;
            double divide = long.MaxValue;

            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                add += i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - add");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                subtract -= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - subtract");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                increment++;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - increment");

            sw.Reset();
            sw.Start();
            for (int i = 1; i <= ONE_MILLION; i++)
            {
                multiply *= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - multiply");

            sw.Reset();
            sw.Start();
            for (int i = 1; i <= ONE_MILLION; i++)
            {
                divide /= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - divide");
        }

        static void Decimal()
        {
            Stopwatch sw = new Stopwatch();

            Console.WriteLine("--- decimal ---");
            decimal add = 1;
            decimal subtract = decimal.MaxValue;
            decimal increment = 3;
            decimal multiply = 1;
            decimal divide = decimal.MaxValue;

            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                add += i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - add");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                subtract -= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - subtract");

            sw.Reset();
            sw.Start();
            for (int i = 0; i < ONE_MILLION; i++)
            {
                increment++;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - increment");

            sw.Reset();
            sw.Start();
            for (int i = 1; i <= ONE_MILLION; i++)
            {
                multiply *= 1;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed+ " - multiply");

            sw.Reset();
            sw.Start();
            for (int i = 1; i <= ONE_MILLION; i++)
            {
                divide /= i;
            }
            sw.Stop();
            Console.WriteLine(sw.Elapsed + " - divide");
        }

        static void Main()
        {
            Int();

            Long();

            Float();

            Double();

            Decimal();
        }
    }
}